package mvp.demo.V.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import mvp.demo.P.MainPresenter;
import mvp.demo.P.IPresenter;
import mvp.demo.R;
import mvp.demo.V.adapter.MainAdapter;

public class MainActivity extends BaseActivity {

    private Dialog loadingDialog;


    private MainAdapter mMainAdapter;
    private View.OnClickListener itemClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadingDialog = new ProgressDialog(this);

    }


    @Override
    protected IPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void setupView() {
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mMainAdapter = new MainAdapter();
        recyclerView.setAdapter(mMainAdapter);


        itemClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof TextView) {
                    if (presenter != null) {
                        ((MainPresenter) presenter).showItem(((TextView) v).getText().toString());
                    }
                }
            }
        };

    }




    @Override
    public void showLoading() {
        loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showItem(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showData(List<String> results) {

        if (mMainAdapter != null) {
            mMainAdapter.init(results, itemClickListener);
            mMainAdapter.notifyDataSetChanged();
        }

    }

    public void goGoGoGo(View view) {
        if (presenter != null) {
            ((MainPresenter) presenter).getData();
        }
    }


}
